<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/12/31
 * Time: 6:47
 */
namespace app\modules\crawler\controllers;

use app\common\controllers\WebController;
use app\common\enum\SetRssSitemapCrawler_statusEnum;
use app\common\enum\SetUrlCrawler_statusEnum;
use app\common\enum\SetUrlCrawler_urlUserEnum;
use app\common\helper\HistoryHelper;
use app\common\service\GptCrawler;
use app\models\SetRssSitemapCrawler;
use app\models\SetUrlCrawler;
use sookie\auth\models\Adminuser;
use Yii;
use yii\helpers\Json;

class IndexController extends \sookie\core\controllers\ApiController
{
    public $debug = false;//调试接口
    //提交链接接口
    public function actionIndex(){
        $url = urldecode(Yii::$app->request->post("url"));
        $match = urldecode(Yii::$app->request->post("match",""));
        $num = intval(Yii::$app->request->post("num","1"));
        $max_tokens = intval(Yii::$app->request->post("max_tokens",2000000));
        if($num<=1){
            $num = 1;
        }
        if($max_tokens < 1000){
            $max_tokens = 1000;
        }
        $row = SetUrlCrawler::find()->andWhere(['url' => $url])->one();
        if($row instanceof SetUrlCrawler){
            if($row->status == SetUrlCrawler_statusEnum::FAIL){
                $row->status = SetUrlCrawler_statusEnum::UNDO;
                $row->save();
                return [
                    'ok' => true,
                    'msg' => "链接提交成功",
                ];
            }
            $this->setStatusCode(403,"链接已提交");
            return [
                'ok' => false,
            ];
        }
        $row = new SetUrlCrawler();
        $row->url = $url;
        $row->max_pages_crawl = $num;
        $row->match = $match;
        $row->output_file_name = GptCrawler::getOutFileName($url);
        $row->max_tokens = $max_tokens;
        $row->status = SetUrlCrawler_statusEnum::UNDO;
        $ok = $row->save();
        if($ok){
            return [
                'ok' => true,
                'msg' => "链接提交成功",
            ];
        }else{
            $this->setStatusCode(500,"链接提交失败");
            return [
                'ok' => false,
            ];
        }
    }

    public function actionSearch(){
        $url = urldecode(Yii::$app->request->post("url"));
        $row = SetUrlCrawler::find()->andWhere(['url' => $url])->one();
        if(!($row instanceof SetUrlCrawler)){
            $this->setStatusCode(410,"记录不存在");
            return [
                'ok' => false
            ];
        }
        if($row->status == SetUrlCrawler_statusEnum::SUCCESS){
            $string = $row->res_url;
            $arr_url = Json::decode($string,true);
            $show_urls = [];
            if($arr_url){
                $show_urls = array_map(function($url){
                    return "http://{$_SERVER['SERVER_NAME']}/api/admin/show/index/index?url=".$url;
                },$arr_url);
                $arr_url = array_map(function($url){
                    return "http://{$_SERVER['SERVER_NAME']}/".$url;
                },$arr_url);
            }

            return [
                'ok' => true,
                'urls' => $arr_url,
                'show_urls' => $show_urls,
            ];
        }
        if($row->status == SetUrlCrawler_statusEnum::FAIL){
            $this->setStatusCode(500,$row->err_msg);
            return [
                'ok' => false,
                'msg' => $row->err_msg,
            ];
        }
        $this->setStatusCode(500,$row->status);
        return [
            'ok' => false,
            'msg' => $row->status,
        ];
    }

    //提交rss接口
    public function actionRss(){
        $url = urldecode(Yii::$app->request->post("url"));
        if(empty($url)){
            $this->setStatusCode(403,"url不能为空");
            return [
                'ok' => false,
            ];
        }
        $day = intval(Yii::$app->request->post("day","1"));
        $max_pages_crawl = intval(Yii::$app->request->post("max_pages_crawl","1"));
        if($day<=1){
            $day = 1;
        }
        if($max_pages_crawl <=1){
            $max_pages_crawl = 1;
        }
        $row = SetRssSitemapCrawler::find()->andWhere(['url' => $url])->one();
        if($row instanceof SetRssSitemapCrawler){
            if(in_array($row->status,[SetRssSitemapCrawler_statusEnum::FAIL,SetRssSitemapCrawler_statusEnum::UNDO])){
                $row->status = SetRssSitemapCrawler_statusEnum::UNDO;
                $row->max_pages_crawl = $max_pages_crawl;
                $row->day = $day;
                $row->save();
                return [
                    'ok' => true,
                    'msg' => "链接提交成功",
                ];
            }
            $this->setStatusCode(403,"链接已提交");
            return [
                'ok' => false,
            ];
        }
        $row = new SetRssSitemapCrawler();
        $row->url = $url;
        $row->max_pages_crawl = $max_pages_crawl;
        $row->day = $day;
        $row->status = SetRssSitemapCrawler_statusEnum::UNDO;
        $ok = $row->save();
        if($ok){
            return [
                'ok' => true,
                'msg' => "链接提交成功",
            ];
        }else{
            $this->setStatusCode(403,"链接提交失败");
            return [
                'ok' => false,
            ];
        }
    }

    public function actionRssSearch(){
        $url = urldecode(Yii::$app->request->post("url"));
        $row_rss = SetRssSitemapCrawler::find()->andWhere(['url' => $url])->one();
        if(!($row_rss instanceof SetRssSitemapCrawler)){
            $this->setStatusCode(410,"记录不存在");
            return [
                'ok' => false
            ];
        }
        if($row_rss->status == SetRssSitemapCrawler_statusEnum::SUCCESS){
            $row = SetUrlCrawler::find()->andWhere(['rss_sitemap_id' => $row_rss->id])
                ->andWhere(['status'=>SetUrlCrawler_statusEnum::SUCCESS])
                ->one();
            if(!($row instanceof SetUrlCrawler)){
                $this->setStatusCode(410,"采集{$row_rss->url}没有可用记录");
                return [
                    'ok' => false
                ];
            }
            $string = $row->res_url;
            $arr_url = Json::decode($string,true);
            $show_urls = [];
            if($arr_url){
                $show_urls = array_map(function($url){
                    return "http://{$_SERVER['SERVER_NAME']}/api/admin/show/index/index?url=".$url;
                },$arr_url);
                $arr_url = array_map(function($url){
                    return "http://{$_SERVER['SERVER_NAME']}/".$url;
                },$arr_url);
            }

            return [
                'ok' => true,
                'urls' => $arr_url,
                'show_urls' => $show_urls,
                'id' => $row->id,
            ];
        }
        if($row_rss->status == SetUrlCrawler_statusEnum::FAIL){
            $this->setStatusCode(500,"采集{$row_rss->url}失败");
            return [
                'ok' => false,
                'msg' => "采集{$row_rss->url}失败",
            ];
        }
        $this->setStatusCode(500,"采集{$row_rss->url}没有可用记录");
        return [
            'ok' => false,
            'msg' => "采集{$row_rss->url}没有可用记录",
        ];
    }

    public function actionRssUse(){
        $id = urldecode(Yii::$app->request->post("id"));
        $row = SetUrlCrawler::find()->andWhere(['id' => $id])->one();
        if($row instanceof SetUrlCrawler){
            $row->used_at = date("Y-m-d H:i:s");
            $row->url_user = SetUrlCrawler_urlUserEnum::USED;
            $ok = $row->save();
            if($ok){
                return [
                    'ok' => true,
                    'msg' => "标记成功",
                ];
            }
            $this->setStatusCode(410,"标记失败");
            return [
                'ok' => false,
                'msg' => "标记失败",
            ];
        }
        $this->setStatusCode(410,"记录不存在");
        return [
            'ok' => false,
            'msg' => "记录不存在",
        ];
    }
}